package com.haojiang.mh.ui.activity

import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.Intent
import android.location.LocationManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.view.View
import com.clj.fastble.BleManager
import com.haojiang.mh.R
import com.haojiang.mh.service.ble.BleHandle
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.ActManager
import com.haojiang.mh.widget.CommonDialog
import com.yanzhenjie.permission.AndPermission
import kotlinx.android.synthetic.main.activity_common_title.*
import kotlinx.android.synthetic.main.activity_device_tutorial.*

/**
 * 连接设备教程
 *
 * Created by ZapFive on 2019-05-26.
 *
 * wuzhuang@mirahome.me
 */
class DeviceTutorialActivity : BaseActivity() {
    override fun getLayoutId() = R.layout.activity_device_tutorial

    override fun getTitleName() = ""

    override fun initActivity() {
    }

    override fun initView() {
        tv_activity_right.visibility = View.VISIBLE
        tv_activity_right.text = getString(R.string.text_skip_connect)
        tv_activity_right.setOnClickListener { onBackClick() }
        tv_next.setOnClickListener {
            checkBluetoothAndPermission()
        }
    }

    override fun onBackClick() {
        ActManager.redirectToBottomActivity()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        when (requestCode) {
            0xAAAA -> showLongToast(getString(R.string.tip_open_bluetooth))
            0xBBBB -> showLongToast(getString(R.string.tip_granted_location))
            0xCCCC -> showLongToast(getString(R.string.tip_open_location))
        }
    }

    //region 蓝牙和位置权限
    /**
     * 蓝牙定位权限
     */
    private fun checkBluetoothAndPermission() {
        if (!isSupportBle) {
            showLongToast(getString(R.string.tip_not_support_ble))
            return
        }
        if (!BleHandle.instance.isBleEnable()) {
            val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
            startActivityForResult(intent, 0xAAAA)
            return
        }
        checkPermission()
    }

    /**
     * 确定权限
     */
    private fun checkPermission() {
        if (AndPermission.hasPermissions(context, Manifest.permission.ACCESS_FINE_LOCATION)) {
            onPermissionGranted()
        } else {
            CommonDialog.Builder(context, 1)
                    .setContent(getString(R.string.tip_granted_location))
                    .setLeftRightClick(
                            { _, _ -> context.finish() },
                            { _, _ ->
                                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                                intent.data = Uri.fromParts("package", context.packageName, null)
                                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                                context.startActivityForResult(intent, 0xBBBB)
                            }
                    )
                    .create().show()
        }
    }

    /**
     * 获取权限
     */
    private fun onPermissionGranted() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !checkGPSIsOpen()) {
            CommonDialog.Builder(context, 1)
                    .setContent(getString(R.string.tip_open_location))
                    .setLeftRightClick(
                            { _, _ -> context.finish() },
                            { _, _ ->
                                val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                                startActivityForResult(intent, 0xCCCC)
                            }
                    )
                    .create().show()
        } else {
            redirectNoParamAndFinish(DeviceListActivity::class.java)
        }
    }

    /**
     * 确定GPS打开
     */
    private fun checkGPSIsOpen(): Boolean {
        val lm = getSystemService(Context.LOCATION_SERVICE) as LocationManager
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
    }
    //endregion
}